﻿using MbUnit.Framework;
using BitTorrent.Bencode.Exceptions;
using BitTorrent.Bencode;

namespace UnitTests.Bencode
{
    /// <summary>
    /// Summary description for UnitTest1
    /// </summary>
    [TestFixture]
    public class IntegerTests
    {
        [Test]
        public void DecodeIntegerOK() {
            var actual = BencodeFactory.Decode(System.Text.Encoding.UTF8.GetBytes("i42e"));
            Assert.IsTrue(actual is Integer);
            Assert.IsTrue(((Integer)actual) == 42);
        }

        [Test]
        [ExpectedException(typeof(BencodeException))]
        public void DecodeIntegerMissingE() {
            var actual = BencodeFactory.Decode(System.Text.Encoding.UTF8.GetBytes("i42"));
        }

        [Test]
        public void DecodeIntegerNegative() {
            var actual = BencodeFactory.Decode(System.Text.Encoding.UTF8.GetBytes("i-3e"));
            Assert.IsTrue(actual is Integer);
            Assert.IsTrue(((Integer)actual) == -3);
        }

        [Test]
        public void DecodeIntegerZero() {
            var actual = BencodeFactory.Decode(System.Text.Encoding.UTF8.GetBytes("i0e"));
            Assert.IsTrue(actual is Integer);
            Assert.IsTrue(((Integer)actual) == 0);
        }

        [Test]
        [ExpectedException(typeof(BencodeException))]
        public void DecodeIntegerNotNumber() {
            var actual = BencodeFactory.Decode(System.Text.Encoding.UTF8.GetBytes("i8Xe"));
        }

        [Test]
        [ExpectedException(typeof(BencodeException))]
        public void DecodeIntegerLeadingZero() {
            var actual = BencodeFactory.Decode(System.Text.Encoding.UTF8.GetBytes("i042e"));
        }
    }
}
